package com.durian.affairs.controller;

import com.durian.affairs.api.domain.CwReceiptPlan;
import com.durian.affairs.service.ICwReceiptPlanService;
import com.durian.common.core.domain.R;
import com.durian.common.core.utils.poi.ExcelUtil;
import com.durian.common.core.web.controller.BaseController;
import com.durian.common.core.web.domain.AjaxResult;
import com.durian.common.core.web.page.TableDataInfo;
import com.durian.common.log.annotation.Log;
import com.durian.common.log.enums.BusinessType;
import com.durian.common.security.annotation.RequiresPermissions;
import com.durian.financial.api.domain.vo.ContractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 收款计划Controller
 * 
 * @author durian
 * @date 2022-06-25
 */
@RestController
@RequestMapping("/cwReceiptPlan")
public class CwReceiptPlanController extends BaseController
{
    @Autowired
    private ICwReceiptPlanService cwReceiptPlanService;

    /**
     * 查询收款计划列表
     */
    @RequiresPermissions("affairs:cwReceiptPlan:list")
    @GetMapping("/list")
    public AjaxResult list(CwReceiptPlan cwReceiptPlan)
    {
        //应收金额
        BigDecimal amountReceivable = BigDecimal.ZERO;
        //实收金额
        BigDecimal amountCollected = BigDecimal.ZERO;
        List<CwReceiptPlan> list2 = cwReceiptPlanService.selectCwReceiptPlanList(cwReceiptPlan);
        for (CwReceiptPlan receiptPlan : list2) {
            if (receiptPlan.getPaymentAmount()!=null){
                amountReceivable=amountReceivable.add(receiptPlan.getPaymentAmount());
            }
            //要获取已经付款的数据 2:已付款
            if (receiptPlan.getSkStatus() != null && receiptPlan.getSkStatus() == 2){
                if (receiptPlan.getPaymentAmount()!=null){
                    amountCollected=amountCollected.add(receiptPlan.getPaymentAmount());
                }
            }
        }
        startPage();
        List<CwReceiptPlan> list = cwReceiptPlanService.selectCwReceiptPlanList(cwReceiptPlan);
        TableDataInfo dataTable = getDataTable(list);
        System.err.println("应收金额总计："+amountReceivable);
        System.err.println("实收金额总计："+amountCollected);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("list",dataTable);
        ajaxResult.put("amountReceivable",amountReceivable);
        ajaxResult.put("amountCollected",amountCollected);
        return ajaxResult;
    }

    /**
     * 导出收款计划列表
     */
    @RequiresPermissions("affairs:cwReceiptPlan:export")
    @Log(title = "收款计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CwReceiptPlan cwReceiptPlan)
    {
        List<CwReceiptPlan> list = cwReceiptPlanService.selectCwReceiptPlanList(cwReceiptPlan);
        ExcelUtil<CwReceiptPlan> util = new ExcelUtil<CwReceiptPlan>(CwReceiptPlan.class);
        util.exportExcel(response, list, "收款计划数据");
    }

    /**
     * 获取收款计划详细信息
     */
    @RequiresPermissions("affairs:cwReceiptPlan:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cwReceiptPlanService.selectCwReceiptPlanById(id));
    }


    /**
     * 根据合同信息获取对应计划列表
     */
    @RequiresPermissions("affairs:cwReceiptPlan:list")
    @GetMapping("/listReceiptPlanByContract")
    public R<List<CwReceiptPlan>> listReceiptPlanByContract (ContractVO vo){

        List<CwReceiptPlan> receiptPlans = cwReceiptPlanService.selectCwReceiptPlanByContract(vo);
        return R.ok(receiptPlans);
    }


    /**
     * 新增收款计划
     */
    @RequiresPermissions("affairs:cwReceiptPlan:add")
    @Log(title = "收款计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CwReceiptPlan cwReceiptPlan)
    {
        return toAjax(cwReceiptPlanService.insertCwReceiptPlan(cwReceiptPlan));
    }

    /**
     * 修改收款计划
     */
    @RequiresPermissions("affairs:cwReceiptPlan:edit")
    @Log(title = "收款计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CwReceiptPlan cwReceiptPlan)
    {
        return toAjax(cwReceiptPlanService.updateCwReceiptPlan(cwReceiptPlan));
    }

    /**
     * 修改收款计划状态
     */
    @RequiresPermissions("affairs:cwReceiptPlan:edit")
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody CwReceiptPlan cwReceiptPlan){

        return toAjax(cwReceiptPlanService.updateStatus(cwReceiptPlan));
    }



    /**
     * 删除收款计划
     */
    @RequiresPermissions("affairs:cwReceiptPlan:remove")
    @Log(title = "收款计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cwReceiptPlanService.deleteCwReceiptPlanByIds(ids));
    }
}
